{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 编程基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数学运算符号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2+3 # 加法\n",
    "4-3 # 减法\n",
    "2*3 # 乘法\n",
    "6/2  # 除法\n",
    "9%2   # 余数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 变量，# 定义变量为了方便多次使用同一内容。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-14.0\n",
      "-28.0\n"
     ]
    }
   ],
   "source": [
    "a=2*3-4*6*7/8+1\n",
    "print(a)\n",
    "print(2*a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据类型 （数字，字符串，列表，元组，字典, 布尔型）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 一、字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I start my graduate study at Nanjing Audit University \n",
      "I start my graduate study at Nanjing Audit University I start my graduate study at Nanjing Audit University \n",
      "I start my graduate study at Nanjing Audit University I love NAU 100 Years\n",
      "I am 22 Years old\n"
     ]
    }
   ],
   "source": [
    "str1=\"I start my graduate study at Nanjing Audit University \" # 字符串首尾用两个双引号或单引号表示。\n",
    "print(str1)\n",
    "print(2*str1) # 复制两遍\n",
    "str1[0:5]    # python 从索引下标从0开始，但不包括右边的下标(5)\n",
    "str1[4:-1] #“-1”表示倒数第一个\n",
    "str1[-1] #“-1”表示倒数第一个\n",
    "len(str1)\n",
    "str2=\"I love NAU \"\n",
    "str1+str2  # 字符串连接方式\n",
    "print(str1+str2+str(100)+\" Years\") # 字符串连接方式\n",
    "str1.count(\"n\") # 出现“n”的个数\n",
    "str1.strip() # 去掉字符串两端的空格字符。\n",
    "age=22\n",
    "print(\"I am {} Years old\".format(age)) # 格式化操作，插入变量\"age\"."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 二、列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 5, 'a', 9, 'c']\n",
      "[1, 2, 99, 'a', 9, 'c']\n",
      "[1, 2, 99, 'a', 9, 'c']\n",
      "[[10, 20]]\n"
     ]
    }
   ],
   "source": [
    "list1=[1,2,5,'a',9,'c'] # or list(1,2,5,'a',9,'c')\n",
    "print(list1)\n",
    "len(list1) # 计数\n",
    "list1[0:2] # 索引\n",
    "list1[2:-1]\n",
    "list1[2]=99 # 改变索引为2处的值\n",
    "print(list1)\n",
    "#list1.append(100) # 等价于 list1=list1+[100]\n",
    "del list1[4] # 删除\n",
    "print(list1)\n",
    "list3=[] # 建立空的列表，以便后续添加元素\n",
    "list3.append([10,20]) # 向原来列表追加新的列表或元素\n",
    "print(list3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 三、元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 3, 'a')\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-46-ed72ec6a76e1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mtuple1\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'a'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 等价于 tuple1=tuple(1,3,'a')\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtuple1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mtuple1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m10\u001b[0m  \u001b[1;31m# 是错误的命令，因为元组不可改变里面元素。\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "tuple1=(1,3,'a') # 等价于 tuple1=tuple(1,3,'a')\n",
    "print(tuple1)\n",
    "tuple1[1]=10  # 是错误的命令，因为元组不可改变里面元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 四、字典\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': ['xiao ming', 'xiao hong'], 'age': [20, 22], 'score': [85, 90], 'address': 'Qin Yuan,Ze Yuan'}\n"
     ]
    }
   ],
   "source": [
    "dict1={\"name\":[\"xiao ming\",\"xiao hong\"],\"age\":[20,22],\"score\":[85,90]} # 字典成对定义的，冒号前面称为'key',后面为“value”.\n",
    "dict1[\"name\"]  # 索引\n",
    "dict1[\"score\"] # 索引\n",
    "dict1[\"address\"]=\"Qin Yuan,Ze Yuan\" # 添加\n",
    "print(dict1)\n",
    "del dict1[\"score\"] # 删除\n",
    "dict1.items() # 以列表返回可遍历的(键, 值) 元组数组\n",
    "dict2={} # 建立空的字典，以便后续添加元素\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 五、布尔型数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 布尔型代表真假值，通常用在条件判断和循环语句中。Python 定义了 bool 类型以及两个常量 True 和 Flase 代表真假"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "bool(2)\n",
    "bool([])\n",
    "print(1<2)\n",
    "print(2>4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 控制语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 一、条件语句 , if--else 句式, if--elif--else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This sentence may be related to Nanjing\n"
     ]
    }
   ],
   "source": [
    "str1=\"I start my graduate study at Nanjing Audit University \"\n",
    "if 'Nanjing' in str1:\n",
    "    print(\"This sentence may be related to Nanjing\")\n",
    "else:\n",
    "    print(\"I cannot make any correct judge\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This sentence may be related to NAU\n"
     ]
    }
   ],
   "source": [
    "if 'nanjing' in str1:\n",
    "    print(\"This sentence may be related to Nanjing\")\n",
    "elif 'nanjing' not in str1 and 'Audit' in str1:\n",
    "    print(\"This sentence may be related to NAU\")\n",
    "else:\n",
    "    print(\"I cannot make any correct judge\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 二、循环语句，for 句式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "0 1 2 3 4 5 6 7 8 9 0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "num=range(10)\n",
    "print(list(num))\n",
    "for i in range(10):\n",
    "    print(i,end=\" \")\n",
    "for i in range(10):\n",
    "    if i % 2==0:\n",
    "        print(i)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常用的python库，numpy 与pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "mat1=np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "print(mat1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         name  age  score\n",
      "0   xiao ming   20     85\n",
      "1   xiao hong   22     90\n",
      "2  xiao qiang   25     80\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>xiao qiang</td>\n",
       "      <td>25</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         name  age  score\n",
       "2  xiao qiang   25     80"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas  as pd\n",
    "dict1={\"name\":[\"xiao ming\",\"xiao hong\",\"xiao qiang\"],\"age\":[20,22,25],\"score\":[85,90,80]}\n",
    "dataset=pd.DataFrame(dict1)\n",
    "print(dataset)\n",
    "display(dataset[dataset['age']>23]) # 查找符合条件的目录\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
